package com.example.lj.uav.utils;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

import com.example.lj.uav.R;

public class AlarmUtils {
    private static long time;
    private static SoundPool soundPool;
    private static int id = -888;
    public static boolean isPlaying;

    public static void playAlarmVideo(Context context){
        long currentTime = System.currentTimeMillis();
        final long t = currentTime-time;
        if (isPlaying){
            return;
        }
        if (t > 1000) {
            soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
            id = soundPool.load(context, R.raw.alarm_limit, 1);
            soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
                @Override
                public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                    soundPool.play(sampleId, 0.8f, 0.8f, 1, -1, 1);
                    isPlaying = true;
                }
            });
            time = currentTime;
        }
    }

    public static void stopAlarm(){
        if (soundPool!=null&&id!=-888){
            if (isPlaying){
                soundPool.stop(id);
                isPlaying = false;
            }

        }
    }
}
